class Menu(object):
    def __init__(self, title, url=None, handler=None):
        self.title = title
        self.url = url
        self.handler = handler
        self.active = False
        self.sub_menus = []

    def add_sub_menu(self, menu):
        self.sub_menus.append(menu)

    def set_selected_path(self, *args):
        try:
            self.active = True
            menu = self
            for p in args:
                for m in menu.sub_menus:
                    m.active = False
                if len(menu.sub_menus) > 0:
                    menu = menu.sub_menus[p]
                    menu.active = True
        except:
            pass

    def to_dict(self):
        _self = {
            'title': self.title,
            'url': self.url,
            'active': self.active,
            'sub_menus': [],
            'handler': self.handler,
        }
        for m in self.sub_menus:
            _self['sub_menus'].append (m.to_dict())

        return _self

